package com.usian.service;

import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.usian.entity.ResultClass;
import com.usian.entity.day14.Emp;
import com.usian.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/*
    Date:2023/8/22
    Author:刘帮祺
*/
@Service
public class Empservice {
    @Autowired
    private EmpFeginService empFeginService;
    @Autowired
    private RedisTemplate redisTemplate;
    public ResultClass login(Emp emp) {
        try {
            ResultClass resultClass = JSON.parseObject(empFeginService.login(emp), ResultClass.class);
            if(resultClass.getCode()==2)return resultClass;
            Object data = resultClass.getData();
            Map<String, Object> map = new HashMap<>();
            map.put("token",data);
            String token = JWTUtil.createToken(map,"token".getBytes());
            redisTemplate.opsForHash().put("用户登录信息",token,JSON.toJSONString(data));
            return ResultClass.ok(token).Message("登录成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResultClass.error().Message("登录失败");
        }
    }
}
